package com.archive.service.impl;

import com.archive.common.*;
import com.archive.entity.*;
import com.archive.mapper.SysUserMapper;
import com.archive.service.SysFacultyService;
import com.archive.service.SysFileService;
import com.archive.service.SysMajorService;
import com.archive.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author Janus
 * @since 2024-03-06
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Value("${com.jane.security.md5.key}")
    private String md5Key;

    @Resource
    private SysFileService fileService;
    @Resource
    private SysFacultyService facultyService;
    @Resource
    private SysMajorService majorService;

    @Override
    public ResponseBean addUser(SysUser user, MultipartFile file) {
        log.debug("addSysUser:"+user);
        if(user==null){
            return  ResultUtil.error(CommonEnum.BAD_PARAM);
        }
        String mobile = user.getMobile();
        if(StringUtils.isEmpty(mobile)||!RegexUtil.checkMobile(mobile)){
            log.error("手机号为空！");
            return  ResultUtil.error("手机号不合法！");
        }
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("code",user.getCode());
        List<SysUser> checkList = list(queryWrapper);
        if(ToolsUtils.isNotEmpty(checkList)){
            return ResultUtil.error("账户已经存在！");
        }
        if(user.getFacultyId()!=null){
            SysFaculty faculty = facultyService.getById(user.getFacultyId());
            user.setFacultyName(faculty.getName());
        }
        if(user.getMajorId()!=null){
            SysMajor major = majorService.getById(user.getMajorId());
            user.setMajorName(major.getName());
        }

        String password= StringUtils.isEmpty(user.getPassword())?"123456":user.getPassword();
        //对密码进行MD5盐值加密，数据库保存密码的密文
        //MD5Util.md5(password,md5Key  md5key就是盐值
        user.setPassword(MD5Util.md5(password,md5Key));
        boolean add = save(user);
        log.debug("用户添加结果："+user);
        //保存文件并更新saveName到user.field1
        if (file!=null&&!file.isEmpty()) {
            SysFile sysFile=new SysFile();
            sysFile.setObjectId(user.getId());
            sysFile.setCategoryCode(ComCodeEnum.category_code_user);
            SysFile saveFile=fileService.saveOrUpdateFile(sysFile,file);
            if(saveFile!=null&&!StringUtils.isEmpty(saveFile.getSaveName())){
                user.setImage(saveFile.getSaveName());
            }
            updateById(user);
        }
       return ResultUtil.successData(user);
    }
}
